//
//  AppsTableViewController.m
//  ksjv2
//
//  Created by mac on 15/1/14.
//  Copyright (c) 2015年 CnbizMedia. All rights reserved.
//

#import "AppsTableViewController.h"
#import "Util.h"
@interface AppsTableViewController (){
    NSMutableArray*_userData;
}

@end

@implementation AppsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD show];
    self.title=@"应用推荐";
    [self loadNews];
    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)loadNews{
    [[AppAPIClient sharedClient] GET:@"System/Apps" parameters:@{@"platform":@"ios"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *result = responseObject[@"data"];
        if([result isKindOfClass:[NSNull class]] || [result count] == 0){
            return;
        }
        if(_userData == nil){
            _userData = [[NSMutableArray alloc]initWithArray:responseObject[@"data"] copyItems:YES];
        }else{
            NSMutableArray *newResult = [[NSMutableArray alloc]initWithArray:_userData];
            [newResult addObjectsFromArray:responseObject[@"data"]];
            _userData = [[NSMutableArray alloc]initWithArray:newResult copyItems:YES];
        }
        [self.tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"ERR: %@",operation.responseString);

    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return _userData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"appcell" forIndexPath:indexPath];
    if (cell==nil) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"appcell"];
    }
    NSDictionary*dic=_userData[indexPath.row];
    
    UIImageView*imageview=(UIImageView*)[cell.contentView viewWithTag:11];
    [imageview sd_setImageWithURL:[NSURL URLWithString:dic[@"thumb"]] placeholderImage:[UIImage imageNamed:@"icon_default_ipad"]];
    
    UILabel*title=(UILabel*)[cell.contentView viewWithTag:22];
    title.text=dic[@"title"];
    
    UILabel*description=(UILabel*)[cell.contentView viewWithTag:33];
    description.text=dic[@"description"];
    
    UIButton*button=(UIButton*)[cell.contentView viewWithTag:55];
    [button.layer setCornerRadius:5];
    [button.layer setMasksToBounds:YES];
    [button.layer setBorderWidth:0.5];
    [button.layer setBorderColor:[UIColor blackColor].CGColor];
    // Configure the cell...
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str2 = [NSString stringWithFormat:@"%@",_userData[indexPath.row][@"url"]];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str2]];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
